<?php

namespace app\admin\controller\mbti;
use think\exception\HttpResponseException;
use think\Response;
use think\Url;
use app\common\controller\Backend;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\response\Json;
/**
 * 场景管理
 *
 * @icon fa fa-circle-o
 */
class Scene extends Backend
{

    /**
     * MbtiScene模型对象
     * @var \app\admin\model\MbtiScene
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\MbtiScene;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("mediaTypeList", $this->model->mediaTypeList());
        $this->view->assign("versionTypeList", $this->model->versionTypeList());
    }



    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);
        foreach ($list as $k => &$val) {
            $v['qr_id'] = $val["id"];
        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function scene_qr()
    {
        $config = get_addon_config('qrcode');
        if (isset($config['limitreferer']) && $config['limitreferer']) {
            $referer = $this->request->server('HTTP_REFERER', '');
            $refererInfo = parse_url($referer);
            $refererHost = $referer && $refererInfo ? $refererInfo['host'] : '';
            $refererHostArr = explode('.', $refererHost);
            $wildcardDomain = '';
            if (count($refererHostArr) > 2) {
                $refererHostArr[0] = '*';
                $wildcardDomain = implode('.', $refererHostArr);
            }
            $allowRefererList = $config['allowrefererlist'] ?? '';
            $domainArr = explode("\n", str_replace("\r", "", $allowRefererList));
            $domainArr = array_filter(array_unique($domainArr));
            $domainArr[] = request()->host(true);
            $inAllowList = false;
            if (in_array('*', $domainArr) || ($refererHost && in_array($refererHost, $domainArr)) || ($wildcardDomain && in_array($wildcardDomain, $domainArr))) {
                $inAllowList = true;
            }
            if (!$inAllowList && (!$referer && $config['allowemptyreferer'])) {
                $inAllowList = true;
            }
            if (!$inAllowList) {
                $response = Response::create('暂无权限', 'html', 403);
                throw new HttpResponseException($response);
            }
        }
        /*
        $params = $this->request->get();
        $params = array_intersect_key($params, array_flip(['text', 'size', 'padding', 'errorlevel', 'foreground', 'background', 'logo', 'logosize', 'logopath', 'label', 'labelmargin', 'labelfontsize', 'labelalignment', 'labelfontcolor']));
        $params['text'] = $this->request->get('text', $config['text'], 'trim');
        $params['label'] = $this->request->get('label', $config['label'], 'trim');
        */
        Url::root('/');
        $url = url('mbti/index/detail', array('scene_id'=> $this->request->get("id", 0, 'intval')),false,true);
        $params = [
            'text' => $url,
            'size' => '300',
            'padding' => '15',
            'errorlevel' => 'medium',
            'foreground' => '#000000',
            'background' => '#ffffff',
            'logo' => '1',
            'logosize' => '50',
            'label' => '',
            'labelmargin' => '0',
            'labelfontsize' => '14',
            'labelalignment' => 'center',
            'labelfontcolor' => '#000000'
        ];
        $qrCode = \addons\qrcode\library\Service::qrcode($params);
        $mimetype = $config['format'] == 'png' ? 'image/png' : 'image/svg+xml';
        $response = Response::create()->header("Content-Type", $mimetype);
        // 直接显示二维码
        header('Content-Type: ' . $qrCode->getMimeType());
        $response->content($qrCode->getString());
        return $response;
    }
}
